package com.zhixiang.wbl.visitor.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhixiang.wbl.visitor.ActivityManager.ScreenManager;
import com.zhixiang.wbl.visitor.R;
import com.zhixiang.wbl.visitor.Utils.QRCodeUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by dongzhixiang on 16/5/25.
 */
public class ShowQRCodeActivity extends Activity
{
    @ViewInject(R.id.id_img_erweima)
    ImageView qrCodeImage;
    @ViewInject(R.id.id_btn_return)
    Button returnButton;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_erweima);
        ViewUtils.inject(this);
        showQRCodeWithMessage("www.baidu.com");
        initReturnButton();
        ScreenManager.getScreenManager().pushActivity(this);
    }

    private void initReturnButton()
    {
        returnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                ScreenManager.getScreenManager().popActivity(ShowQRCodeActivity.this);
               // finish();
            }
        });
    }

    public void showQRCodeWithMessage(final String message)
    {
        final Bitmap bitmap = android.graphics.BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
//        Visitor mVisitor = VisitorUtil.visitor;
//        final Bitmap bitmap = decodeBitmap(mVisitor.getPortrait());
//        if (bitmap==null)
//        {
//            return;
//          //  bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.main_icon_figure);
//        }
        final String filePath = Environment.getExternalStorageDirectory()+ File.separator+"image_qrcode.jpg";
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                boolean isSuccess = QRCodeUtil.createQRImage(message,800,800, bitmap,filePath);
                if (isSuccess)
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run()
                        {
                            Bitmap bitmap1 = BitmapFactory.decodeFile(filePath);
                            qrCodeImage.setImageBitmap(bitmap1);
                        }
                    });
                }
            }
        }).start();
    }
    private Bitmap decodeBitmap(String httpUrl)
    {
        URL url = null;
        Bitmap bm = null;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            InputStream in = url.openStream();
            bm = BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bm;
    }

    @Override
    public void onBackPressed()
    {
        ScreenManager.getScreenManager().popActivity(this);
    }
}
